#include "fig_boss_u14.h"
#include "fighter.h"
#include "fig_bullet.h"
#include "fig_enemybullet_master.h"
#include "fig_player_craft.h"
#include "fig_sounds.h"
#include "fig_boss_laser.h"
#include "fig_boss_attackmove_manager.h"


#include "num/rand.h"
#include "math/facing.h"
#include "dd/dd_man.h"

const float FIGBossU14::LASER_SCALE_WITHOUT_GLOW = 0.4f;

FIGBossU14::FIGBossU14(FIGEnemyCraft::ENEMY_TYPES EnemyType) : 
								FIGEnemyBoss(											
											EnemyType
											),
											m_pBossLaser(NULL),
											m_pRocketShooterLeft(NULL),
											m_pRocketShooterRight(NULL),
											m_pLaserHandLeft(NULL),
											m_pLaserHandRight(NULL)
								
{
	m_ShootCounter.SetLimit(4.0f);
	m_SecondaryWeaponCounter.SetLimit(8.0f);

	
	
}

void		FIGBossU14::OnShootStyle1()
{
	//return;
	bool IsPlus = false;
	float ShootDegree = 90.0f;
	float Delay = 0.0f;

	for(int k = 0; k < 8; k++)
	{
		for( int j = 0; j < 5; j++)
		{
			for(int i =4; i < 5; i++)
			{
				float ShootSide = 0;
				if( i == 4 || i == 5)
					ShootSide = 180.0f;
				else
					ShootSide = 0.0f;

				FIGEnemyBulletMaster::Instance().Shoot(
													this,
													m_Bone.GetPosByWayPoint(i),
													FIGEnemyBulletMaster::BULLET_TYPE_CIRCLE_DOUBLELAYER,
													FIGBullet::SHOOT_STYLES_SINGLE,
													ShootDegree + ShootSide,		// degree
													Delay,
													false,
													NULL,
													false,
													FIGSounds::BOSS_SHOOT
          											);
				
			}
			
			
			Delay += 0.1f;

			if(IsPlus)
				ShootDegree += (180.0f / 5.0f);
			else
				ShootDegree -= (180.0f / 5.0f);

		}

		IsPlus = !IsPlus;
	}
}

void		FIGBossU14::OnShootStyle2()
{
	//return;
	int C = 8;
	float Delay = 0.0f;
	for(int j = 0; j < 8; j++)
	{
		C = 8;
		for(int i = 0; i < C; i++)
		{
			
			FIGEnemyBulletMaster::Instance().Shoot(
												this,
												m_Bone.GetPosByWayPoint(6),
												FIGEnemyBulletMaster::BULLET_TYPE_CIRCLE_TRIPPLELAYER,
												FIGBullet::SHOOT_STYLES_DOUBLE,
												(360.0f / (float)C) * i ,		// degree
												Delay,
												false,
												NULL,
												false,
												FIGSounds::BOSS_SHOOT
          										);
		}

		
		for(int i = 0; i < C; i++)
		{
			
			FIGEnemyBulletMaster::Instance().Shoot(
												this,
												m_Bone.GetPosByWayPoint(7),
												FIGEnemyBulletMaster::BULLET_TYPE_CIRCLE_TRIPPLELAYER,
												FIGBullet::SHOOT_STYLES_DOUBLE,
												(360.0f / (float)C) * i ,		// degree
												Delay,
												false,
												NULL,
												false,
												FIGSounds::BOSS_SHOOT
          										);
		}
		Delay += 0.25f;
		if( j > 0 && (j+1) % 2 == 0)
			Delay += 0.5f;

	}
}

void		FIGBossU14::OnShootStyle3()
{
	//return;
	float Delay = 0.0f;
	for(int j = 0; j < 7; j++)
	{
		for(int i = 6; i < 7; i++)
		{
			FIGEnemyBulletMaster::Instance().Shoot(
													this,
													m_Bone.GetPosByWayPoint(i),
													FIGEnemyBulletMaster::BULLET_TYPE_CIRCLE_GLOW,
													FIGBullet::SHOOT_STYLES_SHAPE_TRIANGLE,
													0 ,		// degree
													Delay,
													true,
													NULL,
													false,
													FIGSounds::BOSS_SHOOT
          											);	
			Delay += 0.4f;
		} 

	}
	
}

void		FIGBossU14::OnShootStyle4()
{
	//return;
	int Count = num::RAND32::Instance().RandInt(5,7);
	for(int i = 0; i < Count;i++)
	{
		
		if(i % 2 == 0)
			FIGEnemyBulletMaster::Instance().Shoot(this,
												m_Bone.GetPosByWayPoint(4), 
												FIGEnemyBulletMaster::BULLET_TYPE_CIRCLE_GLOW,
												FIGBullet::SHOOT_STYLES_RAMBO_5,
												0,
												1.0f * i,
												false,
												NULL ,
												false,
												FIGSounds::BOSS_SHOOT
												);
		else
			FIGEnemyBulletMaster::Instance().Shoot(this,
												m_Bone.GetPosByWayPoint(5), 
												FIGEnemyBulletMaster::BULLET_TYPE_CIRCLE_GLOW,
												FIGBullet::SHOOT_STYLES_RAMBO_5,
												0,
												1.0f * i,
												false,
												NULL,
												false,
												FIGSounds::BOSS_SHOOT
												);
	}
}


void		FIGBossU14::OnSecondaryWeaponShoot()
{
	if(num::RAND32::Instance().RandInt(0,1) == 0)
	{
		OnSecondaryType1();
	}
	else
	{
		OnSecondaryType2();
	}
}


void		FIGBossU14::OnDie()
{
	FIGEnemyBoss::OnDie();
	UTIL_SAFE_DELETE(m_pBossLaser);
	UTIL_SAFE_DELETE(m_pRocketShooterLeft);
	UTIL_SAFE_DELETE(m_pRocketShooterRight);

	UTIL_SAFE_DELETE(m_pLaserHandLeft);
	UTIL_SAFE_DELETE(m_pLaserHandRight);

}

void		FIGBossU14::ReSpawn()
{
	FIGEnemyBoss::ReSpawn();	

	if(!m_pBossLaser)
	{
		m_pBossLaser = new FIGBossLaser(this,
										m_Bone.GetBonePartByID(29),
										m_Bone.GetBonePartByID(30),
										LASER_SCALE_WITHOUT_GLOW,
										ColorfRGBA(1.0f,0.1f,0.1f,0.7f),
										0.4f
										);
	}

	m_pRocketShooterLeft = new RocketShootingBone(	this, 
													m_Bone.GetBonePartByID(17),
													m_Bone.GetPosByWayPoint(1)
													);
	m_pRocketShooterRight = new RocketShootingBone(	this, 
													m_Bone.GetBonePartByID(9),
													m_Bone.GetPosByWayPoint(0)
													);
   /*
   { 
      ID = 21,
      Name = "boss_u14_lasergun_left",
   }, 
   { 
      ID = 22,
      Name = "boss_u14_lasergun_right",
   }, 
   { 
      ID = 23,
      Name = "boss_u14_forehand_left",
   }, 
   { 
      ID = 24,
      Name = "boss_u14_forehand_right",
   }, 
   { 
      ID = 25,
      Name = "boss_u14_arm_left",
   }, 
   { 
      ID = 26,
      Name = "boss_u14_arm_right",
   }, 
   */
	m_pLaserHandLeft = new LaserHand(
										m_Bone.GetBonePartByID(8),     // left arm
										m_Bone.GetBonePartByID(16),		// left forearm
										m_Bone.GetBonePartByID(15),      // left laser
										20.0f,
										20.0f,
										15.0f,
										30.0f
									 );
	m_pLaserHandRight = new LaserHand(
										m_Bone.GetBonePartByID(14),
										m_Bone.GetBonePartByID(5),
										m_Bone.GetBonePartByID(4),
										20.0f,
										20.0f,
										30.0f,
										15.0f
										);


}


void		FIGBossU14::Render(const math::Vec2& Offset,float Scale)
{
	if(m_BossState == BOSS_STATE_ATTACK)
	{
		// right laser
		if(m_pBossLaser)
			m_pBossLaser->Render(Offset,Scale);
	}

	FIGEnemyBoss::Render(Offset,Scale);
}

void		FIGBossU14::RenderDebug(const math::Vec2& Offset,float Scale)
{
	
	FIGEnemyBoss::RenderDebug(Offset,Scale);

	if(m_BossState == BOSS_STATE_ATTACK)
	{
		// right laser
		if(m_pBossLaser)
			m_pBossLaser->RenderDebug(Offset,Scale);
	}
}


void		FIGBossU14::Tick_Attack(float dt)
{
	FIGEnemyBoss::Tick_Attack(dt);

	if(FIGBossAttackMoveManager::Instance().ShouldDisableAttack() == false)
	{
		if(m_pBossLaser)
			m_pBossLaser->Tick(	dt,
							m_pLaserHandRight->GetLaserShootPoint(m_Bone.GetPosByWayPoint(2)),
							m_pLaserHandLeft->GetLaserShootPoint(m_Bone.GetPosByWayPoint(3))
							
							);
	}

	if(FIGBossAttackMoveManager::Instance().ShouldDisableBoneMovement() == false)
	{

		if(m_pRocketShooterLeft)
			m_pRocketShooterLeft->Tick(dt);

		if(m_pRocketShooterRight)
			m_pRocketShooterRight->Tick(dt);

		if(m_pLaserHandLeft)
			m_pLaserHandLeft->Tick(dt);

		if(m_pLaserHandRight)
			m_pLaserHandRight->Tick(dt);
	}
}

void		FIGBossU14::OnSecondaryType1()
{
	if(m_pBossLaser)
		m_pBossLaser->StartWarningAndShoot();
}

void		FIGBossU14::OnSecondaryType2()
{
	m_pRocketShooterLeft->Shoot(8,0.5f);
	m_pRocketShooterRight->Shoot(8,0.5f);

	
}

bool	FIGBossU14::CanTransformBackAttack() const 
{ 
	if( m_pBossLaser &&  
		m_pBossLaser->GetState()  == FIGBossLaser::LASER_STATE_NONE)
		return true; 

	return false;
}







	